<template>
    <view class="container" :style="{ display: visible ? 'block' : 'none' }">
        <view class="work-header" v-if="userInfo">
            <header-bar :config="config"></header-bar>
            <view class="work-header-main">
                <image class="shop-logo" :src="userInfo.HeadImg ? easyGetImgUrl(userInfo.HeadImg) : getDefaultLogo()">
                </image>
                <view class="shop-info">
                    <text class="app-name">{{ userInfo.CompanyName }}</text>
                    <text class="user-name">{{ userInfo.RealName }}</text>
                </view>
                <view v-if="datas && datas.length > 0" class="cut-account" @click="cutAccountFn()">
                    <image class="cut-account-icon" src="@/static/icon/icon_cut_account.png"></image>
                    <text class="cut-account-text">切换</text>
                </view>
            </view>
            <image mode="aspectFill" class="work-header-img" src="@/static/img/img_work_bg.png"></image>
        </view>
        <view class="container-main">
            <view class="container-main-bg"></view>
            <view class="nav-box">
                <view class="nav-item nav-item-purchase" v-for="(item, index) in roleArr" @click="menuClick(item)">
                    <image class="work-item-icon" :src="getMenuIcon(item)"></image>
                    <view class="work-item-bar">
                        <text class="work-item-name">{{ item.FunctionName }}</text>
                        <view class="item-next">
                            <text class="text-icon"></text>
                        </view>
                    </view>
                </view>
                <!-- todo -->
                <view class="nav-item nav-item-purchase" @click="toOfferOrder()">
                    <image class="work-item-icon" src="/pages_work/static/icon/icon_work_item_order.png"></image>
                    <view class="work-item-bar">
                        <text class="work-item-name">报价下单</text>
                        <view class="item-next">
                            <text class="text-icon"></text>
                        </view>
                    </view>
                </view>
                <view class="nav-item nav-item-purchase" @click="test()">
                    <image class="work-item-icon" src="/pages_work/static/icon/icon_work_item_purchase.png"></image>
                    <view class="work-item-bar">
                        <text class="work-item-name">一客一价</text>
                        <view class="item-next">
                            <text class="text-icon"></text>
                        </view>
                    </view>
                </view>
                <!-- <view class="nav-item nav-item-purchase" @click="test()">
					<image class="work-item-icon" src="/pages_work/static/icon/icon_work_item_order.png"></image>
					<view class="work-item-bar">
						<text class="work-item-name">ERP报表(临时)</text>
						<view class="item-next">
							<text class="text-icon"></text>
						</view>
					</view>
				</view> -->
                <!-- <view class="nav-item nav-item-purchase" @click="easyGoTo('/pages_work/packet/purchase/index')" v-if="checkHasAuthority('Purchase_Warehousing',roleArr)">
					<image class="work-item-icon" src="@/pages_work/static/icon/icon_work_item_purchase.png"></image>
					<view class="work-item-bar">
						<text class="work-item-name">采购入库</text>
						<view class="item-next">
							<text class="text-icon"></text>
						</view>
					</view>
				</view> -->
                <!-- <view class="nav-item nav-item-order" @click="easyGoTo('/pages/select/select-customer?selectType=2')"  v-if="checkHasAuthority('Purchase_Order',roleArr)">
					<image class="work-item-icon" src="@/pages_work/static/icon/icon_work_item_order.png"></image>
					<view class="work-item-bar">
						<text class="work-item-name">代下单</text>
						<view class="item-next">
							<text class="text-icon"></text>
						</view>
					</view>
				</view>
				<view class="nav-item nav-item-tms" @click="easyGoTo('/pages_tms/packet/order/list')"  v-if="checkHasAuthority('TMS_Distribution',roleArr)">
					<image class="work-item-icon" src="@/pages_work/static/icon/icon_work_item_tms.png"></image>
					<view class="work-item-bar">
						<text class="work-item-name">TMS配送</text>
						<view class="item-next">
							<text class="text-icon"></text>
						</view>
					</view>
				</view>
				<view class="nav-item nav-item-return" @click="easyGoTo('/pages/select/select-customer?selectType=1')"  v-if="checkHasAuthority('Return_Order',roleArr)">
					<image class="work-item-icon" src="@/pages_work/static/icon/icon_work_item_return.png"></image>
					<view class="work-item-bar">
						<text class="work-item-name">代退单</text>
						<view class="item-next">
							<text class="text-icon"></text>
						</view>
					</view>
				</view>
				<view class="nav-item nav-item-customer" @click="easyGoTo('/pages/customer/add')"  v-if="checkHasAuthority('Customer_Manage',roleArr)">
					<image class="work-item-icon" src="@/pages_work/static/icon/icon_work_item_customer.png"></image>
					<view class="work-item-bar">
						<text class="work-item-name">新增客户</text>
						<view class="item-next">
							<text class="text-icon"></text>
						</view>
					</view>
				</view> -->
            </view>
            <view class="bei-an">闽ICP备15022977号-18A</view>
        </view>
        <pop-check-cut ref="popCheckCut" :datas="datas" @selected="selected"></pop-check-cut>
    </view>
</template>

<script>
import { mapGetters, mapMutations, mapState } from "vuex"
import roleType from "@/type/role.js"
import storage from "@/uni-sub-base/utils/storage.js"

import { GetGuideInfo, GetAdminUserSystemList } from "@/service/api/user.js"
import pic_url from "@/static/img/mebg.png"
import { getGuideInfo } from "@/utils/appInit.js"
import { Login } from "@/service/api/authorize.js"
import { setBaseUrls } from "@/utils/appUrl.js"
import popCheckCut from "@/uni-sub-base/module/cutAccount/cut-account.vue"
import defaultLogo from "@/uni-sub-base/module/cutAccount/icon_cut_default_logo.png"
export default {
    components: {
        popCheckCut,
    },
    props: {
        visible: {
            type: Boolean,
            default: false,
        },
        roleArr: {
            type: Array,
            default: () => [],
        },
    },
    watch: {
        visible: {
            handler(newVal) {
                this.$nextTick(() => { })
            },
            immediate: true,
        },
        roleArr(data) {
            this.$forceUpdate()
        },
    },
    data() {
        return {
            config: {
                title: "",
                back: false,
                centerSlot: true,
                color: ["#ffffff"],
                statusBarFontColor: "#ffffff",
                backgroundColor: "none",
            },
            userInfo: null,
            datas: [],
        }
    },
    mounted() {
        this.userInfo = this.getUserInfo()

        this.getAdminUserSystemList()
    },
    onShow() { },
    methods: {
        ...mapMutations(["SET_AUTH"]),
        goOrder() {
            // uni.switchTab({
            // 	url: '/pages/customer/index'
            // });
            this.$emit("change", "customer")
        },
        checkHasAuthority(role, roleArr) {
            // 订货单 编码- Purchase_Order
            // 退货单 编码- Return_Order
            // 客户管理 编码- Customer_Manage
            // TMS配送 编码- TMS_Distribution
            // 采购入库 编码- Purchase_Warehousing
            // let roleArr = storage.getCache('roleArr')
            if (roleArr) {
                return roleArr.includes(role)
            }
            return false
        },
        cutAccountFn() {
            this.$refs.popCheckCut.openPop()
        },
        selected(item) {
            this.loginByAccount(item)
        },
        loginByAccount(item) {
            if (item.DomainApiName) {
                let businessDomain = item.DomainApiName.includes("http")
                    ? item.DomainApiName
                    : "https://" + item.DomainApiName
                setBaseUrls(businessDomain)
            }
            uni.showLoading({
                title: "切换中",
            })
            let param = {
                type: 1,
                shopId: item.ShopId,
                userId: item.UserId,
            }
            Login(param).then((res) => {
                if (res.State == 1 && res.Data) {
                    let token = res.Data.Token
                    let role = res.Data.PositionFunCodes
                    if (role) {
                        storage.setCache("roleArr", role)
                    }
                    this.SET_AUTH(token)
                    storage.setCache("token", token)
                    storage.setCache("jobPosition", res.Data.JobPosition)
                    let businessDomain = "https://" + res.Data.MobileDomain
                    storage.setCache("businessDomain", businessDomain)
                    uni.hideLoading()
                    storage.removeCache("webInfo")
                    this.getGuideInfoFn()
                } else {
                    uni.hideLoading()
                    this.easyToast(res.Msg)
                }
            })
        },
        getGuideInfoFn() {
            getGuideInfo().then((res) => {
                this.$nextTick(() => {
                    this.easyReLaunch("/pages/index/index")
                })
            })
        },
        getAdminUserSystemList() {
            GetAdminUserSystemList().then((res) => {
                if (res.State == 1 && res.Data && res.Data.length > 0) {
                    res.Data.shift()
                    this.datas = res.Data
                }
            })
        },
        getDefaultLogo() {
            return defaultLogo
        },
        getMenuIcon(item) {
            let icon = ""
            switch (item.FunctionCode) {
                case "ERP_Report":
                    icon = "/pages_work/static/icon/icon_work_item_report.png"
                    break
                case "Purchase_Warehousing":
                    icon = "/pages_work/static/icon/icon_work_item_purchase.png"
                    break
                case "Purchase_Order":
                    icon = "/pages_work/static/icon/icon_work_item_order.png"
                    break
                case "TMS_Distribution":
                    icon = "/pages_work/static/icon/icon_work_item_tms.png"
                    break
                case "Return_Order":
                    icon = "/pages_work/static/icon/icon_work_item_return.png"
                    break
                case "Customer_Manage":
                    icon = "/pages_work/static/icon/icon_work_item_customer.png"
                    break
                case "Warehouse":
                    icon = "/pages_work/static/icon/icon_work_item_warehouse.png"
                    break
                case "stock":
                    icon = "/pages_work/static/icon/icon_work_item_stock.png"
                    break
                default:
                    icon = "/pages_work/static/icon/icon_work_item_purchase.png"
                    break
            }
            return icon
        },
        menuClick(item) {
            let url = ""
            switch (item.FunctionCode) {
                case "stock":
                    url = "/pages/stock/index"
                    break
                case "ERP_Report":
                    url = "/pages_erp/index"
                    break
                case "Purchase_Warehousing":
                    url = "/pages_work/packet/purchase/index"
                    break
                case "Purchase_Order":
                    // url = "/pages/select/select-customer?selectType=2"
                    url = "/pages/customer/replace-order?selectType=2"
                    break
                case "TMS_Distribution":
                    url = "/pages_tms/packet/order/list"
                    break
                case "Return_Order":
                    url = "/pages/select/select-customer?selectType=1"
                    break
                case "Customer_Manage":
                    url = "/pages/customer/add"
                    break
                default:
                    break
            }
            this.easyGoTo(url)
        },
        toOfferOrder() {
            let url = "/pages/customer/offer-order";
            this.easyGoTo(url)
        },
        test() {
            // let url = "/pages_erp/index"

            let url = "/pages/private/index"
            this.easyGoTo(url)
        },
    },
}
</script>

<style lang="scss" scoped>
.container {
    overflow: hidden;
}

.work-header {
    position: relative;
    background: $app-color-main;
    border-bottom-left-radius: 60rpx;

    .work-header-img {
        position: absolute;
        width: 100%;
        height: 276rpx;
        bottom: 0;
        right: 40rpx;
        z-index: 1;
    }

    .work-header-main {
        position: relative;
        z-index: 2;
        display: flex;
        flex-direction: row;
        align-items: center;
        padding: 20rpx 20rpx 40rpx 40rpx;

        .shop-logo {
            width: 120rpx;
            height: 120rpx;
            border-radius: 120rpx;
            background: #ffffff;
            margin-right: 20rpx;
            border: 4rpx solid #ffffff;
        }

        .shop-info {
            display: flex;
            flex-direction: column;

            text {
                display: flex;
                color: #ffffff;
            }

            .app-name {
                font-size: $font-size-40;
                font-weight: bold;
                margin-bottom: 32rpx;
            }

            .user-name {
                font-size: $font-size-28;
            }
        }
    }
}

.container-main {
    // background: $app-color-main;
}

.container-main-bg {
    height: 70rpx;
    background: $app-color-main;
    margin-top: -2rpx;
}

.nav-box {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 40rpx;
    border-top-right-radius: 60rpx;
    position: relative;
    z-index: 2;
    background: #f7f7f7;
    margin-top: -72rpx;
}

.nav-item {
    width: 240rpx;
    height: 150rpx;
    padding: 36rpx;
    background: $color-white;
    border-radius: 32rpx;
    display: flex;
    flex-direction: column;
    margin-bottom: 36rpx;

    .work-item-icon {
        width: 80rpx;
        height: 80rpx;
        margin-bottom: 24rpx;
    }

    .work-item-bar {
        display: flex;
        flex-direction: row;
        justify-content: space-between;

        .work-item-name {
            font-size: $font-size-30;
        }

        .item-next {
            width: 40rpx;
            height: 40rpx;
            border-radius: 40rpx;
            display: flex;
            justify-content: center;
            align-items: center;

            .text-icon {
                width: 24rpx;
                height: 24rpx;
                background-image: url(@/pages_work/static/icon/icon_work_item_right.png);
            }
        }
    }
}

.nav-item-purchase {
    .item-next {
        background-color: #fef4f4;
    }
}

.nav-item-order {
    .item-next {
        background-color: #eff9f7;
    }
}

.nav-item-tms {
    .item-next {
        background-color: #f2f6fe;
    }
}

.nav-item-return {
    .item-next {
        background-color: #f4f2fe;
    }
}

.nav-item-customer {
    .item-next {
        background-color: #fef4f4;
    }
}

.icon-work-tms {
    width: 100rpx;
    height: 100rpx;
    margin-bottom: -20rpx;
    background-image: url(@/static/icon/icon_work_tms.jpg);
}

.cut-account {
    position: absolute;
    right: 40rpx;
    top: 20rpx;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    width: 116rpx;
    height: 52rpx;

    background: rgba(255, 255, 255, 0.4);
    border-radius: 8rpx;
    border: 1rpx solid rgba(255, 255, 255, 0.6);
    backdrop-filter: blur(20rpx);

    .cut-account-icon {
        width: 30rpx;
        height: 30rpx;
        margin-right: 8rpx;
        display: flex;
    }

    .cut-account-text {
        font-size: $font-size-24;
        color: $color-white;
    }
}

.bei-an {
    position: fixed;
    bottom: 104rpx;
    text-align: center;
    width: 100%;
    left: 0;
}
</style>
